package awweaver.clanplugin;

import java.io.File;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.Event.Type;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.config.Configuration;

public class ClanPlugin extends JavaPlugin {
    public static final Logger logger = Logger.getLogger("Minecraft");
    static final String mainDirectory = "plugins/ClanWars"; 
    static File yml = new File(mainDirectory + File.separator + "config.yml"); 
    static Properties prop = new Properties(); 
    
    Configuration config = getConfiguration();
    //protected static final String mainDirectory = "plugins/ClanWars";
    
    //protected static Properties prop = new Properties()
    Clan clan;
    private Player player;
    //private PlayerListener playerListener;
    
    public void loadProcedure(){
			config.load();
                        config.setProperty("Hello", true);       
                        config.save();
    }
    
    
    public void onDisable() {
        // Place any custom disable code here.
        config.save();
        ClanPlugin.logger.log(Level.INFO, this + " is now disabled!", this);
        
    }
    public void onEnable()
    {
        //ClanPlugin.logger.log(Level.INFO, getDataFolder().toString(), this);
       new File(mainDirectory).mkdir();
            if(!yml.exists()){
			try {
				yml.createNewFile();
				//FileOutputStream out = new FileOutputStream(yml);
				config.setProperty("ZoneCount", "0");
				config.setHeader("Do not edit this File");
				config.save();
			} catch (IOException ex) {
			}
		} else { 
			loadProcedure();
		}
        
        config.load();
        
        ClanPlugin.logger.log(Level.INFO, this + " is now enabled!", this);
        
        getCommand("criminal").setExecutor(new CommandExecutor() {
            public boolean onCommand(CommandSender cs, Command cmnd, String string, String[] strings) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });
        getCommand("police").setExecutor(new CommandExecutor() {
            public boolean onCommand(CommandSender cs, Command cmnd, String string, String[] strings) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });
        getCommand("military").setExecutor(new CommandExecutor() {
            public boolean onCommand(CommandSender cs, Command cmnd, String string, String[] strings) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });
        getCommand("mecenaries").setExecutor(new CommandExecutor() {
            public boolean onCommand(CommandSender cs, Command cmnd, String string, String[] strings) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });
        
        
        getCommand("clanwars").setExecutor(new CommandExecutor() {
           public boolean onCommand(CommandSender cs, Command cmd, String alias, String[] args) {
               player = (Player)cs;
               if("awweaver".equals(player.getName()))
               {
                   getServer().broadcastMessage("The plugin creator for Clan Wars has joined the server, say Hi!" );
               }
               return true;
           }
        });
             
        getCommand("clan").setExecutor(new CommandExecutor() {
           public boolean onCommand(CommandSender cs, Command cmd, String alias, String[] args) {
               player = (Player)cs;
               
               if(args.length == 0)
               {
                   player.sendMessage("This command requires arguments.");
                   return false;
               }
               else if(!(cs instanceof Player))
               {
                   player.sendMessage("You are not a player");
                   return false;
               }
               else if ("formclan".equals(args[0].toString()))
               {
                   clan = new Clan(args[1].toString(), player);
                   return true;
               }
               else if ("addmember".equals(args[0].toString()) && clan.isLeader(player))
               {
                   clan.addMember(args[1].toString(), player);
                   return true;
               }
               else if (("apply".equals(args[0].toString()) || "join".equals(args[0].toString())))
               {
                   clan.applyToClan(args[1].toString(), player);
                   return true;
               }
               else if ("declarewar".equals(args[0].toString()) || "war".equals(args[0].toString()))
               {
                   clan.applyToClan(args[1].toString(), player);
                   return true;
               }
               else if ("changetype".equals(args[0].toString()) || "type".equals(args[0].toString()))
               {
                   if(args[2].isEmpty())
                   {
                       clan.changeType(args[1].toString(), player);
                   }
                   else
                   {
                       clan.changetType(args[1].toString(), player, args[2].toString());
                   }
                   
                   return true;
               }
               else if ("rename".equals(args[0].toString()) && clan.isLeader(player))
               {
                   clan.renameClan(args[1].toString());
                   return true;
               }
               else if ("listclans".equals(args[0].toString()))
               {
                   clan.listClans();
                   return true;
               }
               else if ("listmembers".equals(args[0].toString()))
               {
                   clan.listMembers(args[1].toString());
                   return true;
               }
               else if ("promote".equals(args[0].toString()) && clan.isLeader(player))
               {
                   clan.promoteMember(args[1].toString(), args[2].toString());
                   return true;
               }
               else if ("demote".equals(args[0].toString()) && clan.isLeader(player))
               {
                   clan.demoteMember(args[1].toString(), args[2].toString());
                   return true;
               }
               else if ("disband".equals(args[0].toString()) && clan.isLeader(player))
               {
                   clan.disbandClan(player);
                   return true;
               }
               else if ("invite".equals(args[0].toString()) && clan.isLeader(player))
               {
                   clan.inviteMember(args[1].toString());
                   return true;
               }
               else if ("arena".equals(args[0].toString()))
               {
                   clan.createArena();
                   return true;
               }
               else if ("tag".equals(args[0].toString()) || "clantag".equals(args[0].toString()))
               {
                   clan.setClanTag(args[1].toString(), player);
                   return true;
               }  
               return false;
           }
        });
    }   
}